package com.trust.pams.dao.service.impl;

import com.chasonx.common.model.PageResult;
import com.mongodb.client.result.DeleteResult;
import com.trust.pams.dao.mongo.BaseMongoDaoImpl;
import com.trust.pams.dao.service.PamsPuoteDao;
import com.trust.pams.dto.PamsQuoteDto;
import com.trust.pams.model.Tree.Tree;
import com.trust.pams.model.query.PamsQuoteQuery;
import com.trust.pams.util.DataUtil;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PamsPuoteDaoImpl extends BaseMongoDaoImpl<PamsQuoteDto> implements PamsPuoteDao {
    @Override
    public int batchPamsPuote(List<PamsQuoteDto> pamsquote) {
        super.insertAll(pamsquote);
        return 1;
    }

    public DeleteResult deletePamsPuote(String projectGuid){
        Query query = new Query();
        Criteria criteria = Criteria.where("projectGuid").is(projectGuid);
        query.addCriteria(criteria);
        return super.getMongoTemplate().remove(query,PamsQuoteDto.class);
    }

    public PageResult<PamsQuoteDto> listPamsQuotes(PamsQuoteQuery pamsQuoteQuery) {
        Query dbQuery = new Query();
        if(DataUtil.isNotEmpty(pamsQuoteQuery.getLastMonth())){
            dbQuery.addCriteria(Criteria.where("rows.创建时间").gt(pamsQuoteQuery.getLastMonth()));
        }
        dbQuery.skip( (pamsQuoteQuery.getPageNumber() - 1) * pamsQuoteQuery.getPageSize()).limit(pamsQuoteQuery.getPageSize());
        long count = getMongoTemplate().count(dbQuery, getCollectionName());
        List<PamsQuoteDto> listData = find(dbQuery);

        PageResult<PamsQuoteDto> page = new PageResult<>();
        page.setTotalRow(count);
        page.setData(listData);
        return page;
    }

    public PageResult<PamsQuoteDto> listPamsQuote(PamsQuoteQuery pamsQuoteQuery) {
        Query dbQuery = new Query();
        if(DataUtil.isNotEmpty(pamsQuoteQuery.getProjectName())){
            dbQuery.addCriteria(Criteria.where("projectName").is(pamsQuoteQuery.getProjectName()).and(""));

        }
        long count = getMongoTemplate().count(dbQuery, getCollectionName());
        dbQuery.skip( (pamsQuoteQuery.getPageNumber() - 1) * pamsQuoteQuery.getPageSize()).limit(pamsQuoteQuery.getPageSize());
        List<PamsQuoteDto> listData = find(dbQuery);


        PageResult<PamsQuoteDto> page = new PageResult<>();
        page.setTotalRow(count);
        page.setData(listData);
        return page;
    }

    @Override
    public List<PamsQuoteDto> getPamsQuote(String projectGuid) {
        Query dbQuery = new Query();
            dbQuery.addCriteria(Criteria.where("id").is(projectGuid));
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }

    @Override
    public List<PamsQuoteDto> allPamsQuote() {
        Query dbQuery = new Query();
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }

    @Override
    public List<PamsQuoteDto> getNamePamsQuote(String key,String name){
        Query dbQuery= new Query();
        dbQuery.addCriteria(Criteria.where(key).regex(name));
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }

    public List<PamsQuoteDto> getNamePamsQuotes(String key,String name){
        Query dbQuery= new Query();
            dbQuery.addCriteria(Criteria.where("rows."+key).regex(name));
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }

    public List<PamsQuoteDto> getConditionPamsQuotes(String key,String name,String projectId,String creationTime){
        Query dbQuery= new Query();
        if(DataUtil.isNotEmpty(projectId)&&DataUtil.isNotEmpty(creationTime)){
            dbQuery.addCriteria(Criteria.where("rows."+key).regex(name).and("projectId").is(projectId).and("rows.创建时间").regex(creationTime));
        }else if(DataUtil.isNotEmpty(projectId)){
            dbQuery.addCriteria(Criteria.where("rows."+key).regex(name).and("projectId").is(projectId));
        }else if (DataUtil.isNotEmpty(creationTime)){
            dbQuery.addCriteria(Criteria.where("rows."+key).regex(name).and("rows.创建时间").regex(creationTime));
        }
        else{
            dbQuery.addCriteria(Criteria.where("rows."+key).regex(name));
        }
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }

    public  List<PamsQuoteDto> getClientPamsQuote(String clientName){
        Query dbQuery= new Query();
        dbQuery.addCriteria(Criteria.where("clientName").is(clientName));
        List<PamsQuoteDto> listData = find(dbQuery);
        return listData;
    }
}
